import {
  serializeJsonAppUserData,
  getAppUserDataLocalStorage
} from './serializer';

const getBooleanInt = s => (typeof s === 'boolean' ? (s && 1) || 0 : 0);
const appendParenthesis = callbackName => `${callbackName}()`;

export default [
  // 隐藏加载动画
  {
    methodName: 'hideLoading',
    android: {
      interfaceMethod: 'hideLodingView',
      paramSerializer: (_, sender) => sender()
    },
    ios: {
      interfaceMethod: 'hideLodingView',
      paramSerializer: (_, sender) => sender('close')
    }
  },

  // 埋点
  {
    methodName: 'report',
    defaultParams: { action: '', value: {} },
    android: {
      interfaceMethod: 'onEvent',
      rootInterface: 'LifeDetailInterface',
      paramSerializer: ({ action, value }, sender) =>
        sender(action, JSON.stringify(value))
    },
    ios: {
      interfaceMethod: 'onEvent',
      paramSerializer: ({ action, value }, sender) =>
        sender({ event_id: action, label: JSON.stringify(value) })
    }
  },

  // 显示分享菜单
  {
    methodName: 'openShareMenu',
    defaultParams: {
      title: '',
      text: '',
      imageUrl: '',
      link: ''
    },
    android: {
      interfaceMethod: 'moreShareOption',
      paramSerializer: ['title', 'text', 'imageUrl', 'link']
    },
    ios: {
      interfaceMethod: 'moreShareOption',
      paramSerializer: (options, sender) =>
        sender([options.title, options.text, options.imageUrl, options.link])
    }
  },

  // 跳转
  {
    methodName: 'jump',
    defaultParams: {
      type: 1, // 1 APP 2 浏览器 3 不跳转
      nativeRedirect: 16, // 0-生活页面 16-资讯
      url: '',
      pageType: 1, // 1-资讯页面 2-生活页面 3-其他页面 4-H5页面
      newsType: 0, // Number 类型, 字符类型在 ios 下不会生效
      newsId: 0,
      title: '',
      description: '',
      thumb: ''
    },
    interfaceMethod: 'jumpByParameter',
    paramSerializer: (options, sender) => sender(JSON.stringify(options))
  },

  // 跳转到话题
  {
    methodName: 'jumpToTopic',
    defaultParams: {
      id: '',
      title: ''
    },
    interfaceMethod: 'goTopicDetail',
    android: {
      paramSerializer: ['id', 'title']
    },
    ios: {
      paramSerializer: (options, sender) => sender(options)
    }
  },

  // 禁止向左滑动退出
  {
    methodName: 'setSwipeEnable',
    interfaceMethod: 'setSwipeEnable',
    defaultParams: {
      enable: false
    },
    android: {
      paramSerializer: ({ enable }, sender) => sender(getBooleanInt(enable))
    },
    ios: {
      paramSerializer: ({ enable }, sender) => sender(getBooleanInt(enable))
    }
  },

  // 获取设备、token等信息
  {
    methodName: 'getAppUserData',
    paramSerializer: (_, sender) => sender(),
    android: {
      interfaceMethod: 'getUserData',
      resultSerializer: serializeJsonAppUserData
    },
    ios: {
      interfaceMethod: () => getAppUserDataLocalStorage,
      resultSerializer: serializeJsonAppUserData
    }
  },

  // 登陆
  {
    methodName: 'login',
    async: true,
    defaultParams: { isShowMsg: false },
    android: {
      interfaceMethod: 'openLoginActivity',
      paramSerializer: ({ callbackName }, sender) => sender(callbackName)
    },
    ios: {
      interfaceMethod: 'reLogin',
      paramSerializer: ({ callbackName, isShowMsg }, sender) =>
        sender({ callBack: appendParenthesis(callbackName), isShowMsg })
    }
  },

  //打开一个非同源链接的webview
  {
    methodName: 'goUrl',
    defaultParams: {
      url: ''
    },
    android: {
      interfaceMethod: 'goUrl',
      rootInterface: 'LifeDetailInterface',
      paramSerializer: (options, sender) => sender('', options.url)
    },
    ios: {
      interfaceMethod: 'getItemUrl',
      paramSerializer: (options, sender) => sender(options.url)
    }
  },
  // 单独第三方分享方法 type===== 1：facebook  2：google 3：Twitter 4：line 5：微信朋友圈 6：微信好友 7：qq 8：qq空间 9：messenger
  {
    methodName: 'shareByType',
    defaultParams: {
      title: '',
      text: '',
      imageUrl: '',
      link: '',
      type: ''
    },
    android: {
      interfaceMethod: 'shareByType',
      paramSerializer: ['title', 'text', 'imageUrl', 'link', 'type']
    },
    ios: {
      interfaceMethod: 'shareByType',
      paramSerializer: (options, sender) =>
        sender([
          options.title,
          options.text,
          options.imageUrl,
          options.link,
          options.type
        ])
    }
  },
  // 登录方法
  {
    methodName: 'reLogin',
    defaultParams: {
      callBack: '',
      isShowMsg: false
    },
    android: {
      interfaceMethod: 'openLoginActivity',
      paramSerializer: (options, sender) => sender(options.callBack)
    },
    ios: {
      interfaceMethod: 'reLogin',
      paramSerializer: (options, sender) =>
        sender({
          callBack: options.callBack !== '' ? options.callBack + '()' : '',
          isShowMsg: options.isShowMsg
        })
    }
  },

  // 跳转到话题
  {
    methodName: 'goTopicPublishNewsFeed',
    defaultParams: {
      topicId: 0,
      topicTitle: ''
    },
    android: {
      interfaceMethod: 'goTopicPublishNewsFeed',
      paramSerializer: ['topicId', 'topicTitle']
    },
    ios: {
      interfaceMethod: 'goTopicPublishNewsFeed',
      paramSerializer: (options, sender) => sender(options)
    }
  }
];
